/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.UserFaxPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  UserFaxPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class UserFaxSqlProvider {
	public static final String TABLE_NAME = "user_fax";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param userFax
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserFaxs(final UserFaxPo userFax) {
		return new CustomerSQL() {{
			SELECT("bank,bank_account,create_time,creator_id,id,invoice_content,invoice_title,invoice_type,reg_address,reg_phone,taxpayer_code,update_time,updater_id,user_id ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userFax.getBank())) {
				WHERE("bank=#{bank}");
			}
			if (StringUtils.isNotBlank(userFax.getBankAccount())) {
				WHERE("bank_account=#{bankAccount}");
			}
			if (null != userFax.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceContent())) {
				WHERE("invoice_content=#{invoiceContent}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceTitle())) {
				WHERE("invoice_title=#{invoiceTitle}");
			}
			if (null != userFax.getInvoiceType()) {
				WHERE("invoice_type=#{invoiceType}");
			}
			if (StringUtils.isNotBlank(userFax.getRegAddress())) {
				WHERE("reg_address=#{regAddress}");
			}
			if (StringUtils.isNotBlank(userFax.getRegPhone())) {
				WHERE("reg_phone=#{regPhone}");
			}
			if (StringUtils.isNotBlank(userFax.getTaxpayerCode())) {
				WHERE("taxpayer_code=#{taxpayerCode}");
			}
			if (null != userFax.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != userFax.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if(StringUtils.isNotBlank(userFax.getSortField())) {
				ORDER_BY(userFax.getSortField() + (StringUtils.isNotBlank(userFax.getSortMethod()) ? "desc" : userFax.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != userFax.getStart() && null != userFax.getSize()) {
				LIMIT(userFax.getStart(), userFax.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param userFax
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserFaxCount(final UserFaxPo userFax) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(userFax.getBank())) {
				WHERE("bank=#{bank}");
			}
			if (StringUtils.isNotBlank(userFax.getBankAccount())) {
				WHERE("bank_account=#{bankAccount}");
			}
			if (null != userFax.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceContent())) {
				WHERE("invoice_content=#{invoiceContent}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceTitle())) {
				WHERE("invoice_title=#{invoiceTitle}");
			}
			if (null != userFax.getInvoiceType()) {
				WHERE("invoice_type=#{invoiceType}");
			}
			if (StringUtils.isNotBlank(userFax.getRegAddress())) {
				WHERE("reg_address=#{regAddress}");
			}
			if (StringUtils.isNotBlank(userFax.getRegPhone())) {
				WHERE("reg_phone=#{regPhone}");
			}
			if (StringUtils.isNotBlank(userFax.getTaxpayerCode())) {
				WHERE("taxpayer_code=#{taxpayerCode}");
			}
			if (null != userFax.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != userFax.getUserId()) {
				WHERE("user_id=#{userId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserFaxById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param userFax
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addUserFax(final UserFaxPo userFax) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(userFax.getBank())) {
				VALUES("bank","#{bank}");
			}
			if (StringUtils.isNotBlank(userFax.getBankAccount())) {
				VALUES("bank_account","#{bankAccount}");
			}
			if (null != userFax.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceContent())) {
				VALUES("invoice_content","#{invoiceContent}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceTitle())) {
				VALUES("invoice_title","#{invoiceTitle}");
			}
			if (null != userFax.getInvoiceType()) {
				VALUES("invoice_type","#{invoiceType}");
			}
			if (StringUtils.isNotBlank(userFax.getRegAddress())) {
				VALUES("reg_address","#{regAddress}");
			}
			if (StringUtils.isNotBlank(userFax.getRegPhone())) {
				VALUES("reg_phone","#{regPhone}");
			}
			if (StringUtils.isNotBlank(userFax.getTaxpayerCode())) {
				VALUES("taxpayer_code","#{taxpayerCode}");
			}
			if (null != userFax.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != userFax.getUserId()) {
				VALUES("user_id","#{userId}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param userFax
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateUserFax(final UserFaxPo userFax) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userFax.getBank())) {
				SET("bank=#{bank}");
			}
			if (StringUtils.isNotBlank(userFax.getBankAccount())) {
				SET("bank_account=#{bankAccount}");
			}
			if (null != userFax.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceContent())) {
				SET("invoice_content=#{invoiceContent}");
			}
			if (StringUtils.isNotBlank(userFax.getInvoiceTitle())) {
				SET("invoice_title=#{invoiceTitle}");
			}
			if (null != userFax.getInvoiceType()) {
				SET("invoice_type=#{invoiceType}");
			}
			if (StringUtils.isNotBlank(userFax.getRegAddress())) {
				SET("reg_address=#{regAddress}");
			}
			if (StringUtils.isNotBlank(userFax.getRegPhone())) {
				SET("reg_phone=#{regPhone}");
			}
			if (StringUtils.isNotBlank(userFax.getTaxpayerCode())) {
				SET("taxpayer_code=#{taxpayerCode}");
			}
			if (null != userFax.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != userFax.getUserId()) {
				SET("user_id=#{userId}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delUserFax(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
